﻿/// <reference path="../../../../Sustainalytics.Core.UI/Scripts/typings/angularjs/angular.d.ts" />
module Sustainalytics.EsgRatings.Admin {

    import Common = Sustainalytics.Common;
    import CommonAdmin = Sustainalytics.Common.Admin;
    import AdminAccounts = Sustainalytics.Accounts.Admin;
    import HttpPromise = ng.IHttpPromise;

    export class ESGProfileBackendService implements IESGProfileBackendService {

        private $q: ng.IQService;
        private $http: ng.IHttpService;
        private notificationFactory: Common.INotificationFactory;
        private staticObjectsAdmin: CommonAdmin.IStaticObjects;
        private $location: ng.ILocationService;

        constructor(
            $q: ng.IQService,
            $http: ng.IHttpService,
            $location: ng.ILocationService,
            notificationFactory: Common.INotificationFactory,
            staticObjectsAdmin: CommonAdmin.IStaticObjects) {

            var ctx = this;
            ctx.$q = $q;
            ctx.$http = $http;
            ctx.notificationFactory = notificationFactory;
            ctx.staticObjectsAdmin = staticObjectsAdmin;
            ctx.$location = $location;
        }

        //To delete when replaced into controller
        //public loadProfiles(defaultOnly:boolean): ng.IPromise<ESGProfileOutDto> { 
        //    var ctx = this;

        //    return ctx.$http.get(ctx.servUrl(), { params: { defaultOnly: defaultOnly } } );
        //}

        public loadDefaultEsgProfile(): ng.IPromise<ESGProfileDto>{
            var ctx = this;
            var def = this.$q.defer();
           
            //account/{accountId}/profiles
            //var promise = ctx.$http.get("/esgratingserv/api/v1/esgprofile/account?" + $.param({ accountId: null }) + "/profiles");
            var promise = ctx.$http.get('/esgratingserv/api/v1/esgprofile/account/' + null  + '/profiles');

            promise
                .success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                def.reject({ data: data, status: status, headers: headers, config: config });
            });

            return def.promise;
        }

        public listEsgProfileByAccountId(accountId: string): ng.IPromise<ESGProfileDto[]> {
            var ctx = this;
            var def = this.$q.defer();           
           
            var promise = ctx.$http.get('/esgratingserv/api/v1/esgprofile/account/'+ accountId + '/profiles');

            promise
                .success(function (data) {
                def.resolve(data);
            })
                .error(function (data, status, headers, config) {
                def.reject({ data: data, status: status, headers: headers, config: config });
            });

            return def.promise;
        }

        public saveProfile(dto: ESGProfileDto): ng.IHttpPromise<ESGProfileDto> {
            var ctx = this;
            //var def = this.$q.defer();


            //var promise = ctx.$http.put<ESGProfileDto>(ctx.servUrl(), dto);
            var promise = ctx.$http.put<ESGProfileDto>(ctx.servUrl(), dto);

            //promise.success(function (data) { def.resolve(data); }).error(function (err) {
            //    ctx.notificationFactory.error("", "Error updating custom weight matrix.");
            //    def.reject();
            //})
            //return def.promise;

            return promise;
            //  return ctx.$http.patch<ESGProfileDto>(ctx.servUrl(), dto);// replace
        }
        
        public addProfile(dto: ESGProfileDto): ng.IHttpPromise<ESGProfileDto> {
            var ctx = this;
            var promise = ctx.$http.post<ESGProfileDto>(ctx.servUrl(), dto);
            return promise;
        } 

        public deleteProfile(profId: string): ng.IHttpPromise<any> {
            var ctx = this;
            var url: string = '/esgratingserv/api/v1/esgprofile/profile/' + profId;
            var promise = ctx.$http.delete(url);
            return promise;
        }

        public deleteProfiles(accountId: string, activeState: ActiveTypeEnum ): ng.IHttpPromise<any> {
            var ctx = this;
            var url: string = '/esgratingserv/api/v1/esgprofile/account/' + accountId;
            var promise = ctx.$http.delete(url);
            return promise;
        }

        private servUrl(): string {
            var ctx = this;
            return ctx.$location.protocol() + "://" + ctx.$location.host() + ":" + ctx.$location.port() +
                "/esgratingserv/api/v1/esgprofile/";
        }
    }


    // register service with EsgRatings Angular module
    var mod = angular.module(esgRatingsAdminModuleName);

    mod.service('ESGProfileBackendService',
        ['$q', '$http', '$location', 'notificationFactory', 'staticObjectsAdmin', ESGProfileBackendService]);
}